<div class="layuimini-main">
	<div class="layui-form layuimini-form" lay-filter="form-data">
		<div class="layui-form-item">
			<label class="layui-form-label required">仓库类型</label>
			<div class="layui-input-block">
				<select name="type" id="type">
					<option value="">请选择</option>
					<option value="GitLab">GitLab</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">仓库地址</label>
			<div class="layui-input-block">
				<input type="text" name="url" lay-verify="required" lay-reqtext="仓库地址不能为空" placeholder="如：https://jihulab.com" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">认证方式</label>
			<div class="layui-input-block">
				<select name="authType" lay-filter="authType" lay-verify="required" lay-reqtext="认证类型不能为空">
					<option value="">请选择</option>
					<option value="1">令牌认证</option>
					<option value="2">账号认证</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item" style="display: none" id="authTokenDiv">
			<label class="layui-form-label required">认证令牌</label>
			<div class="layui-input-block">
				<input type="text" name="authToken" placeholder="请输入认证令牌" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item" style="display: none" id="authNameDiv">
			<label class="layui-form-label required">认证名称</label>
			<div class="layui-input-block">
				<input type="text" name="authName" placeholder="请输入认证名称" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item" style="display: none" id="authPasswordDiv">
			<label class="layui-form-label required">认证密码</label>
			<div class="layui-input-block">
				<input type="password" name="authPassword" placeholder="请输入认证密码" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
			</div>
		</div>
	</div>
<div>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

		$.ajax({
			url: '/globalConfig/query',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({"itemTypes": [2]}),
			success: function(res){
				if(res.code != "000000"){
					layer.msg(res.message, {icon: 5, shift: 6});
					return false;
				}
				if(JSON.stringify(res.data) === '{}'){
					return false;
				}
				//给表单赋值
				form.val("form-data", {
				  "type": res.data.codeRepo.type
				  ,"url": res.data.codeRepo.url
				  ,"authType": res.data.codeRepo.authType
				  ,"authToken": res.data.codeRepo.authToken
				  ,"authName": res.data.codeRepo.authName
				  ,"authPassword": res.data.codeRepo.authPassword
				});
				
				if(res.data.codeRepo.authType == 1){
					$("#authTokenDiv").show();
				}else if(res.data.codeRepo.authType == 2){
					$("#authNameDiv").show();
					$("#authPasswordDiv").show();
				}
			},
			error: function(res){
				layer.msg(res.message, {icon: 5, shift: 6});
			}
		});
		
		form.render('select');
		
		form.on('select(authType)', function(data) {
			if(data.value == 1){
				$("#authTokenDiv").show();
				$("#authNameDiv").hide();
				$("#authPasswordDiv").hide();
			}else if(data.value == 2){
				$("#authTokenDiv").hide();
				$("#authNameDiv").show();
				$("#authPasswordDiv").show();
			}else{
				$("#authTokenDiv").hide();
				$("#authNameDiv").hide();
				$("#authPasswordDiv").hide();
			}
		});
		
        //监听提交
        form.on('submit(saveBtn)', function (data) {
			if(data.field.url.substr(0, 4) !== 'http'){
				layer.msg('仓库地址格式不正确', {icon: 5, shift: 6});
				return false;
			}
			if(data.field.authType == 1){
				if(data.field.authToken == ''){
					layer.msg("认证令牌不能为空", {icon: 5, shift: 6});
					return false;
				}
				data.field.authName = '';
				data.field.authPassword = '';
			}else if(data.field.authType == 2){
				if(data.field.authName == '' || data.field.authPassword == ''){
					layer.msg("认证名称和密码不能为空", {icon: 5, shift: 6});
					return false;
				}
				data.field.authToken = ''
			}
			//增加类型
			data.field.itemType = 2;
			$.ajax({
				url: '/globalConfig/codeRepo/addOrUpdate',
				type: 'POST',
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify(data.field),
				success: function(res){
					if(res.code != "000000"){
						layer.msg(res.message, {icon: 5, shift: 6});
						return false;
					}	
					layer.msg("保存成功", {
						icon: 1,
						time: 700,
						shade: 0.01,
						shadeClose: false
					});
				},
				error: function(res){
					layer.msg(res.message, {icon: 5, shift: 6});
				}
			});

            return false;
        });

    });
</script>